﻿Imports System.Globalization

Public Class frmCompras

    Dim controller As New ControllerSigeco.controller_compras
    Dim controllerStock As New ControllerSigeco.controller_stock

    Private Sub cbxVto_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxVto.CheckedChanged
        fechaVto.Enabled = cbxVto.Checked
    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()
    End Sub

    Private _codNotaRemision As Integer = 0

    Public Property codNotaRemision() As Integer
        Get
            Return _codNotaRemision
        End Get
        Set(ByVal value As Integer)
            _codNotaRemision = value
        End Set
    End Property

    Private _codProveedor As Integer = 0

    Public Property codProveedor() As Integer
        Get
            Return _codProveedor
        End Get
        Set(ByVal value As Integer)
            _codProveedor = value
        End Set
    End Property

    Private _codMercaderia As Integer = 0

    Public Property codMercaderia() As Integer
        Get
            Return _codMercaderia
        End Get
        Set(ByVal value As Integer)
            _codMercaderia = value
        End Set
    End Property

    Private _codMercaderiaDetalle As Integer = 0

    Public Property codMercaderiaDetalle() As Integer
        Get
            Return _codMercaderiaDetalle
        End Get
        Set(ByVal value As Integer)
            _codMercaderiaDetalle = value
        End Set
    End Property

    Private _codCompra As Integer = 0

    Public Property codCompra() As Integer
        Get
            Return _codCompra
        End Get
        Set(ByVal value As Integer)
            _codCompra = value
        End Set
    End Property

    Private _codOrdenCompra As Integer = 0
    Public Property codOrdenCompra() As Integer
        Get
            Return _codOrdenCompra
        End Get
        Set(ByVal value As Integer)
            _codOrdenCompra = value
        End Set
    End Property

    Private _soloLectura As Boolean = False
    Public Property soloLectura() As Boolean
        Get
            Return _soloLectura
        End Get
        Set(ByVal value As Boolean)
            _soloLectura = value
        End Set
    End Property

    Private m_frmBusMercaderia As frmMercaderiaBuscador
    Public Property frmBusMercaderia() As frmMercaderiaBuscador
        Get
            Return m_frmBusMercaderia
        End Get
        Set(ByVal value As frmMercaderiaBuscador)
            m_frmBusMercaderia = value
        End Set
    End Property

    Private m_frmBusProveedor As frmProveedorBuscador
    Public Property frmBusProveedor() As frmProveedorBuscador
        Get
            Return m_frmBusProveedor
        End Get
        Set(ByVal value As frmProveedorBuscador)
            m_frmBusProveedor = value
        End Set
    End Property

    Private m_frmBusOrden As frmOrdenCompraBuscador
    Public Property frmBusOrden() As frmOrdenCompraBuscador
        Get
            Return m_frmBusOrden
        End Get
        Set(ByVal value As frmOrdenCompraBuscador)
            m_frmBusOrden = value
        End Set
    End Property

    Private Sub btnBuscProveedor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscProveedor.Click

        frmBusProveedor = New frmProveedorBuscador
        frmBusProveedor.ShowDialog()
        txtProveedor.Text = frmBusProveedor.ProveedorDesc
        codProveedor = frmBusProveedor.codProveedor
        If codProveedor > 0 Then
            btnBuscMercaderia.Focus()
        End If

    End Sub

    Private Sub btnBuscOrdenCompra_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscOrdenCompra.Click

        Try

            If ValidarCampos(False) Then
                Exit Sub
            End If

            frmBusOrden = New frmOrdenCompraBuscador
            frmBusOrden.ShowDialog()
            txtOrdenCompra.Text = frmBusOrden.nroOrden
            codOrdenCompra = frmBusOrden.codOrden
            If codOrdenCompra > 0 Then

                Dim dt As New DataTable
                dt = controller.get_Orden_Compras_ByID_Compras(oiml.dbConn, codOrdenCompra)

                If Not dt Is Nothing Then
                    If dt.Rows.Count > 0 Then
                        If MessageBox.Show("Esta seguro de factura la orden de compra nro.?", "Orden de compra", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then

                            codOrdenCompra = dt.Rows(0)("idOrdenCompra")
                            txtProveedor.Text = dt.Rows(0)("proveedor")
                            codProveedor = dt.Rows(0)("idProveedor")
                            cboTipo.Text = dt.Rows(0)("condicion")

                            If ValidarCampos(True) Then
                                Exit Sub
                            End If

                            If codCompra = 0 Then
                                codCompra = controller.Insert_Compras(oiml.dbConn, nroFactura.Text, fechaFactura.Value, _
                                                                      codProveedor, fechaVto.Value, cboTipo.Text, _
                                                                      cboEstado.Text, codOrdenCompra, 0, txtTotalFactura.Text, _
                                                                      txtSaldo.Text, txtIVA.Text, oiml.idUsuario, codNotaRemision)
                            Else
                                If controller.Update_Compras(oiml.dbConn, codCompra, nroFactura.Text, fechaFactura.Value, _
                                                                      codProveedor, fechaVto.Value, cboTipo.Text, _
                                                                      cboEstado.Text, codOrdenCompra, 0, txtTotalFactura.Text, _
                                                                      txtSaldo.Text, txtIVA.Text, oiml.idUsuario, codNotaRemision) Then
                                Else
                                    MessageBox.Show("Error: " & controller.MessageError.Message, "Compras", MessageBoxButtons.OK, MessageBoxIcon.Error)

                                End If

                            End If

                            If codCompra = 0 Then
                                Exit Sub
                            End If

                            Dim dtDet As New DataTable
                            dtDet = controller.get_Orden_Compras_Detalles_All(oiml.dbConn, codOrdenCompra)

                            If Not dtDet Is Nothing Then
                                For i As Integer = 0 To dtDet.Rows.Count - 1
                                    If controller.Insert_Compras_Detalle(oiml.dbConn, codCompra, dtDet.Rows(i)("idMercaderia"), dtDet.Rows(i)("cantidad"), dtDet.Rows(i)("precio"), cboIVA.SelectedValue, dtDet.Rows(i)("total"), oiml.idUsuario, cbxNoPosee.Checked, txtLote.Text, "", _
                                        cboDeposito.SelectedValue, codNotaRemision, "", 0, False) Then

                                    Else
                                        MessageBox.Show("Error: " & controller.MessageError.Message, "Compras", MessageBoxButtons.OK, MessageBoxIcon.Error)
                                    End If

                                Next

                                CargarCompras()
                                MessageBox.Show("Orden de compra, registrada con exito.!", "Orden de compra", MessageBoxButtons.OK, MessageBoxIcon.Information)

                            End If

                        End If

                    End If

                Else
                    MessageBox.Show("Error: Orden de compra ya fue utilizada o no posee confirmacion.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

                End If

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub btnBuscMercaderia_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscMercaderia.Click

        frmBusMercaderia = New frmMercaderiaBuscador
        frmBusMercaderia.ShowDialog()
        txtMercaderia.Text = frmBusMercaderia.MercaderiaDesc
        codMercaderia = frmBusMercaderia.codMercaderia
        If codMercaderia > 0 Then
            txtcantidad.Focus()
        End If

    End Sub

    Private Sub txtcantidad_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtcantidad.Leave
        If IsNumeric(txtcantidad.Text) And IsNumeric(txtprecio.Text) Then
            txttotal.Text = txtcantidad.Text * txtprecio.Text
        End If
    End Sub

    Private Sub frmCompras_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dgvdetalle.AutoGenerateColumns = False
        btnImprimir.Visible = False
        cboTipo.SelectedIndex = 0
        cboEstado.SelectedIndex = 0

        oiml.CargarCombos(cboIVA, "Impuesto", "idImpuesto", controllerStock.get_impuestos(oiml.dbConn, 0, ""))
        oiml.CargarCombos(cboDeposito, "descripcion", "idDeposito", controllerStock.get_depositos(oiml.dbConn, 0, "", "", True))

        CargarCompras()

    End Sub

    Private Sub CargarComprasDetalle()
        Try

            codMercaderia = 0
            codMercaderiaDetalle = 0
            txtMercaderia.Text = ""
            txtcantidad.Text = 1
            txtprecio.Text = 0
            txttotal.Text = 0

            Dim ds As New DataTable
            ds = controller.get_Compras_Detalles_All(oiml.dbConn, codCompra)

            If Not ds Is Nothing Then
                dgvdetalle.DataSource = ds

                If ds.Rows.Count > 0 And codNotaRemision = 0 Then
                    cbxNoPosee.Visible = False
                End If

            Else
                dgvdetalle.DataSource = Nothing
            End If

            txtMercaderia.Focus()

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Dim ci As CultureInfo

    Private Sub CargarCompras()
        Try
            If codCompra > 0 Then

                Dim ds As New DataTable
                ds = controller.get_Compras_ByID(oiml.dbConn, codCompra)

                If Not ds Is Nothing Then

                    If Not ds.Rows(0)("idNotaRemision") Is DBNull.Value Then
                        cbxNoPosee.Checked = False
                        cbxNoPosee.Visible = False

                        codNotaRemision = ds.Rows(0)("idNotaRemision")
                        txtNroRemision.Text = ds.Rows(0)("nroNotaRemision")
                        txtNroRemision.ReadOnly = True
                        btnBuscRemision.Visible = False
                    End If

                    txtActualizadoFecha.Text = ds.Rows(0)("actualizadoFecha_")
                    txtActualizadoPor.Text = ds.Rows(0)("actualizadoPor")

                    If Not ds.Rows(0)("nroOrden") Is DBNull.Value Then
                        codOrdenCompra = ds.Rows(0)("idOrdenCompra")
                        txtOrdenCompra.Text = ds.Rows(0)("nroOrden")
                    End If

                    'ci = Thread.CurrentThread.CurrentCulture

                    txtProveedor.Text = ds.Rows(0)("proveedor")
                    codProveedor = ds.Rows(0)("idProveedor")
                    txtSaldo.Text = Decimal.Parse(ds.Rows(0)("totalFacturaSaldo").ToString).ToString("N0")
                    'ds.Rows(0)("totalFacturaSaldo").ToString("D")
                    txtTotalFactura.Text = Decimal.Parse(ds.Rows(0)("totalFactura").ToString).ToString("N0")
                    'ds.Rows(0)("totalFactura").ToString("D")
                    txtIVA.Text = Decimal.Parse(ds.Rows(0)("totalFacturaIVA").ToString).ToString("N0")
                    'ds.Rows(0)("totalFacturaIVA").ToString("D")
                    nroFactura.Text = ds.Rows(0)("nroComprobante")
                    fechaFactura.Value = ds.Rows(0)("fecha")

                    If Not ds.Rows(0)("fechaVencimiento") Is DBNull.Value Then
                        fechaVto.Value = ds.Rows(0)("fechaVencimiento")
                    End If

                    cboEstado.Text = ds.Rows(0)("estado")
                    cboTipo.Text = ds.Rows(0)("condicion")

                    ControlEstados()

                    'btnGrabar.Enabled = True
                    'btnEliminar.Enabled = True

                    '******************************************************************************
                    'If cboTipo.Text = "CONTADO" And (ds.Rows(0)("estado") = "APROBADA" Or ds.Rows(0)("estado") = "ANULADA") Then
                    '    btnRegistrarCobro.Visible = True
                    'Else
                    '    btnRegistrarCobro.Visible = False
                    'End If

                    CargarComprasDetalle()

                End If

            Else
                cboEstado.Text = "EN PROCESO"
                nroFactura.Focus()

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub ControlEstados()

        If cboEstado.Text = "APROBADA" Then

            nroFactura.Enabled = False
            fechaFactura.Enabled = False
            fechaVto.Enabled = False
            cboTipo.Enabled = False

            btnBuscMercaderia.Enabled = False
            btnBuscProveedor.Enabled = False
            btnBuscOrdenCompra.Enabled = False

            btnGrabar.Enabled = False
            btnAgregarDetalle.Enabled = False

            btnConfirmar.Visible = False

            btnEliminar.Enabled = False
            btnEliminarDetalle.Enabled = False

            txtcantidad.Enabled = False
            txtprecio.Enabled = False
            cboIVA.Enabled = False

            btnCancelar.Focus()

        ElseIf cboEstado.Text = "ANULADA" Or soloLectura Then

            nroFactura.Enabled = False
            fechaFactura.Enabled = False
            fechaVto.Enabled = False
            cboTipo.Enabled = False

            btnBuscMercaderia.Enabled = False
            btnBuscProveedor.Enabled = False
            btnBuscOrdenCompra.Enabled = False

            btnGrabar.Enabled = False
            btnAgregarDetalle.Enabled = False
            btnEliminarDetalle.Enabled = False
            btnConfirmar.Enabled = False
            btnEliminar.Enabled = False
            btnAnular.Enabled = False

            txtcantidad.Enabled = False
            txtprecio.Enabled = False
            cboIVA.Enabled = False

            btnCancelar.Focus()

        End If

    End Sub

    Private Function ValidarCampos(ByVal conProveedor As Boolean) As Boolean

        Dim controlCampos As Boolean = False

        If nroFactura.Text = "" Or nroFactura.Text.Replace("-", "").Replace("_", "").Trim() = "" Then
            ErrorProvider1.SetError(nroFactura, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(nroFactura, "")
        End If        '

        If txtProveedor.Text = "" And conProveedor Then
            ErrorProvider1.SetError(txtProveedor, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(txtProveedor, "")
        End If        '

        If cboTipo.Text = "CREDITO" Then
            If (fechaFactura.Value > fechaVto.Value) Then
                ErrorProvider1.SetError(fechaVto, "La fecha de Vencimiento debe ser mayor a la fecha de registro")
                controlCampos = True
            Else
                ErrorProvider1.SetError(fechaVto, "")
            End If        '
        Else
            ErrorProvider1.SetError(fechaVto, "")
        End If

        Return controlCampos

    End Function

    Private Function ValidarCamposDetalle() As Boolean

        Dim controlCampos As Boolean = False

        If txtMercaderia.Text = "" Then
            ErrorProvider1.SetError(txtMercaderia, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(txtMercaderia, "")
        End If        '

        If txtcantidad.Text = "" Or Not IsNumeric(txtcantidad.Text) Then
            ErrorProvider1.SetError(txtcantidad, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(txtcantidad, "")
        End If        '

        If txtprecio.Text = "" Or Not IsNumeric(txtprecio.Text) Then
            ErrorProvider1.SetError(txtprecio, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(txtprecio, "")
        End If        '

        Return controlCampos

    End Function

    Private Sub btnEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminar.Click
        If codCompra > 0 Then

            If MessageBox.Show("Desea eliminar el registro seleccionado?", "Compras", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then

                If controller.Delete_Compras(oiml.dbConn, codCompra, oiml.idUsuario) Then
                    Me.Close()

                Else
                    MessageBox.Show("Error: " & controller.MessageError.Message, "Compras", MessageBoxButtons.OK, MessageBoxIcon.Error)

                End If

            End If

        End If

    End Sub

    Private Sub btnGrabar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGrabar.Click

        If ValidarCampos(True) Then
            Exit Sub
        End If

        Dim fechaVencimiento As String = ""

        If cbxVto.Checked Then
            fechaVencimiento = fechaVto.Value
        End If

        If codCompra = 0 Then
            codCompra = controller.Insert_Compras(oiml.dbConn, nroFactura.Text, fechaFactura.Value, _
                                                  codProveedor, fechaVencimiento, cboTipo.Text, _
                                                  cboEstado.Text, codOrdenCompra, 0, txtTotalFactura.Text, _
                                                  txtSaldo.Text, txtIVA.Text, oiml.idUsuario, codNotaRemision)

            If codCompra > 0 Then
                CargarCompras()
                MessageBox.Show("Registro grabado con exito!", "Compras", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

            Else
                MessageBox.Show("Error: " & controller.MessageError.Message, "Compras", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        Else
            If controller.Update_Compras(oiml.dbConn, codCompra, nroFactura.Text, fechaFactura.Value, _
                                                  codProveedor, fechaVencimiento, cboTipo.Text, _
                                                  cboEstado.Text, codOrdenCompra, 0, txtTotalFactura.Text, _
                                                  txtSaldo.Text, txtIVA.Text, oiml.idUsuario, codNotaRemision) Then
                CargarCompras()
                MessageBox.Show("Registro grabado con exito!", "Compras", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)


            Else
                MessageBox.Show("Error: " & controller.MessageError.Message, "Compras", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        End If

    End Sub

    Private Sub dgvdetalle_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgvdetalle.KeyDown
        If e.KeyCode = Keys.Enter Then
            If dgvdetalle.SelectedRows.Count > 0 Then

                codMercaderiaDetalle = dgvdetalle.SelectedRows(0).Cells(0).Value

            End If
        End If
    End Sub

    Private Sub dgvdetalle_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvdetalle.DoubleClick
        If dgvdetalle.SelectedRows.Count > 0 Then

            codMercaderiaDetalle = dgvdetalle.SelectedRows(0).Cells(0).Value

        End If
    End Sub

    Private Sub dgvdetalle_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvdetalle.CellClick
        Try

            If dgvdetalle.SelectedRows.Count > 0 Then

                codMercaderiaDetalle = dgvdetalle.SelectedRows(0).Cells(0).Value

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btnEliminarDetalle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminarDetalle.Click

        If codMercaderiaDetalle > 0 Then

            If MessageBox.Show("Desea eliminar el registro seleccionado?", "Compras", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then

                If controller.Delete_Compras_Detalle(oiml.dbConn, codMercaderiaDetalle, oiml.idUsuario, codCompra) Then
                    CargarCompras()

                Else
                    MessageBox.Show("Error: " & controller.MessageError.Message, "Compras", MessageBoxButtons.OK, MessageBoxIcon.Error)

                End If

            End If

        End If

    End Sub

    Private Sub btnAgregarDetalle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAgregarDetalle.Click

        If ValidarCampos(True) Then
            Exit Sub
        End If

        If ValidarCamposDetalle() Then
            Exit Sub
        End If

        If codCompra = 0 Then
            codCompra = controller.Insert_Compras(oiml.dbConn, nroFactura.Text, fechaFactura.Value, _
                                                  codProveedor, fechaVto.Value, cboTipo.Text, _
                                                  cboEstado.Text, codOrdenCompra, 0, txtTotalFactura.Text, _
                                                  txtSaldo.Text, txtIVA.Text, oiml.idUsuario, codNotaRemision)
        Else
            If controller.Update_Compras(oiml.dbConn, codCompra, nroFactura.Text, fechaFactura.Value, _
                                                  codProveedor, fechaVto.Value, cboTipo.Text, _
                                                  cboEstado.Text, codOrdenCompra, 0, txtTotalFactura.Text, _
                                                  txtSaldo.Text, txtIVA.Text, oiml.idUsuario, codNotaRemision) Then
            Else
                MessageBox.Show("Error: " & controller.MessageError.Message, "Compras", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        End If

        If codCompra > 0 And codMercaderia > 0 Then
            Dim idDeposito As Integer = 0
            If cbxNoPosee.Checked Then
                idDeposito = cboDeposito.SelectedValue
            End If

            Dim vtoMercaderia As String = ""
            If cbxVtoMercaderia.Checked Then
                vtoMercaderia = vencimientoMercaderia.Value
            End If

            If controller.Insert_Compras_Detalle(oiml.dbConn, codCompra, codMercaderia, _
                                                    txtcantidad.Text, txtprecio.Text, _
                                                    cboIVA.SelectedValue, txttotal.Text, _
                                                    oiml.idUsuario, cbxNoPosee.Checked, txtLote.Text, _
                                                    vtoMercaderia, idDeposito, codNotaRemision, "", _
                                                    0, False) Then
                CargarCompras()
                MessageBox.Show("Registro grabado con exito!", "Compras", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Else
                MessageBox.Show("Error: " & controller.MessageError.Message, "Compras", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

        End If

    End Sub

    Private Sub btnConfirmar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConfirmar.Click

        If codCompra > 0 Then

            If controller.Update_Compras_Estado(oiml.dbConn, codCompra, "APROBADA", oiml.idUsuario) Then
                CargarCompras()
                MessageBox.Show("Registro confirmado con exito!", "Compras", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Else
                MessageBox.Show("Error: " & controller.MessageError.Message, "Compras", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

        End If

    End Sub

    Private Sub cboTipo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboTipo.SelectedIndexChanged

        If cboTipo.Text = "CREDITO" Then
            cbxVto.Checked = True
        Else
            cbxVto.Checked = False
        End If

    End Sub

    Private Sub txtprecio_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtprecio.Leave
        If IsNumeric(txtcantidad.Text) And IsNumeric(txtprecio.Text) Then
            txttotal.Text = txtcantidad.Text * txtprecio.Text
        End If
    End Sub

    Private Sub btnAnular_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnular.Click
        If codCompra > 0 Then
            If MessageBox.Show("Desea anular el registro seleccionado?", "Compras", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then

                If controller.Update_Compras_Estado(oiml.dbConn, codCompra, "ANULADA", oiml.idUsuario) Then
                    Me.Close()

                Else
                    MessageBox.Show("Error: " & controller.MessageError.Message, "Compras", MessageBoxButtons.OK, MessageBoxIcon.Error)

                End If

            End If
        End If
    End Sub

    Private Sub cbxVtoMercaderia_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxVtoMercaderia.CheckedChanged
        vencimientoMercaderia.Enabled = cbxVtoMercaderia.Checked
    End Sub

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxNoPosee.CheckedChanged

        btnBuscProveedor.Enabled = cbxNoPosee.Checked

        cboDeposito.Enabled = cbxNoPosee.Checked
        txtLote.Enabled = cbxNoPosee.Checked
        cbxVtoMercaderia.Enabled = cbxNoPosee.Checked

        If Not cbxNoPosee.Checked Then
            cbxVtoMercaderia.Checked = False
            txtLote.Text = ""
            codProveedor = 0
            txtProveedor.Text = ""
        End If

        lblRemision.Visible = Not cbxNoPosee.Checked
        txtNroRemision.Visible = Not cbxNoPosee.Checked
        txtNroRemision.Text = ""
        txtNroRemision.Enabled = Not cbxNoPosee.Checked
        btnBuscRemision.Visible = Not cbxNoPosee.Checked


    End Sub

    Private Sub btnBuscRemision_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscRemision.Click

        If txtNroRemision.Text <> "" Then

            Dim dsRemision As New DataTable
            dsRemision = controller.get_Remision_All(oiml.dbConn, txtNroRemision.Text, DateTime.Now, DateTime.Now, "", "", 0, "APROBADA", 0, 0)

            If Not dsRemision Is Nothing Then
                codProveedor = dsRemision.Rows(0)("idProveedor")
                txtProveedor.Text = dsRemision.Rows(0)("Proveedor")
                btnBuscProveedor.Enabled = False

                txtNroRemision.Text = dsRemision.Rows(0)("nroComprobante")
                codNotaRemision = dsRemision.Rows(0)("idNotaRemision")
                txtNroRemision.Enabled = False
                MessageBox.Show("Mensaje: Registro de remision, relacionada con exito.", "Compras", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

            Else

                If controller.MessageError Is Nothing Then
                    MessageBox.Show("Error: No se puede encontrar la remision declarada, Favor verifique.", "Compras", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Else
                    MessageBox.Show("Error: " & controller.MessageError.Message, "Compras", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If

            End If

        End If


    End Sub

    Private Sub txtMercaderia_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtMercaderia.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarMercaderias()
        End If
    End Sub

    Private Sub CargarMercaderias()
        Try
            Dim idMarca As Integer = 0
            Dim idRubro As Integer = 0
            Dim ds As New DataTable

            ds = controllerStock.get_mercaderias_All(oiml.dbConn, 0, "", idMarca, idRubro, False, txtMercaderia.Text, 0, 0)

            If Not ds Is Nothing Then

                txtMercaderia.Text = ""
                txtMercaderia.Text = ds.Rows(0)("mercaderia")
                codMercaderia = ds.Rows(0)("idMercaderia")
                If codMercaderia > 0 Then
                    txtcantidad.Focus()
                End If

            Else

                txtMercaderia.Text = ""
                codMercaderia = 0
                MessageBox.Show("Codigo no registrado, favor verifique", "Mercaderias", MessageBoxButtons.OK, MessageBoxIcon.Information)

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub txtMercaderia_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMercaderia.Enter
        txtMercaderia.Text = ""
        codMercaderia = 0
    End Sub

    Private Sub txtOrdenCompra_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtOrdenCompra.KeyDown
        If e.KeyCode = Keys.Enter Then
            btnBuscOrdenCompra_Click(sender, e)
        End If

    End Sub

    Private m_frmRegistroCobro As frmComprasCobro
    Public Property frmRegistroCobro() As frmComprasCobro
        Get
            Return m_frmRegistroCobro
        End Get
        Set(ByVal value As frmComprasCobro)
            m_frmRegistroCobro = value
        End Set
    End Property

    Private Sub btnRegistrarCobro_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRegistrarCobro.Click

        If codCompra > 0 And codProveedor > 0 And cboEstado.Text = "APROBADA" And cboTipo.Text = "CONTADO" Then

            frmRegistroCobro = New frmComprasCobro
            frmRegistroCobro.codCompra = codCompra
            frmRegistroCobro.codProveedor = codProveedor
            frmRegistroCobro.ShowDialog()
            CargarCompras()

        End If

    End Sub

    'codigo para textbox numerico--------------inicio-------------------
    Private Sub txtprecio_changed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtprecio.TextChanged
        If IsNumeric(txtcantidad.Text) And IsNumeric(txtprecio.Text) Then

            If txtprecio.Text = "" Then
                txtprecio.Text = Nothing
                textWithcomma = "0"
                'this property maintain the content of textbox with comma
                'this property maintain the content of textbox without comma
                textWithoutcomma = "0"
            Else
                If txtprecio.Text <> "" Then
                    Dim d As Double = Convert.ToDouble(skipComma(txtprecio.Text))
                    txtprecio.Text = d.ToString("N0") 'd.ToString("#.#", System.Globalization.CultureInfo.InvariantCulture)
                    textWithcomma = txtprecio.Text
                    'property maintain content of textbox with comma
                    'property maintain content of textbox without comma
                    textWithoutcomma = skipComma(txtprecio.Text)
                End If
            End If

            txtprecio.[Select](txtprecio.Text.Length, 0)

        End If
    End Sub

    Public textWithcomma As String
    Public textWithoutcomma As String

    Public Function skipComma(ByVal str As String) As String
        Dim ss As String() = Nothing
        Dim strnew As String = ""
        If str = "" Then
            strnew = "0"
        Else
            ss = str.Split("."c)
            For i As Integer = 0 To ss.Length - 1
                strnew += ss(i)
            Next
        End If
        Return strnew
    End Function

    Private Sub txtprecio_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtprecio.KeyPress
        If Not Char.IsControl(e.KeyChar) AndAlso Not Char.IsDigit(e.KeyChar) AndAlso (e.KeyChar <> "."c) Then
            e.Handled = True
        End If

        ' only allow one decimal point
        If (e.KeyChar = "."c) AndAlso (TryCast(sender, TextBox).Text.IndexOf("."c) > -1) Then
            e.Handled = True
        End If

    End Sub
    'codigo para textbox numerico--------------inicio-------------------


End Class